<template>
  <div class="myContailer">
    <el-row class="myTitleBorderBottom">
      <el-col :span="12" class="myLineHeight_40px">{{this.title}}</el-col>
      <el-col :span="12" class="myTextAlignRight">
        <el-button type="primary" class="myBgGreen" @click="add" v-if="isShow(122)">新 增</el-button>
        <!-- <el-button type="primary" class="myBgGreen">导出Excel</el-button> -->
      </el-col>
    </el-row>
    <el-row class="myMarginTop_10px" :gutter="10">
      <el-col :span="6">
        <template>
          <el-cascader
            change-on-select
            placeholder="全部项目/小区"
            :options="regionOptions"
            v-model="regionOptSelected"
            @change="changeRegionOpt"
          ></el-cascader>
        </template>
      </el-col>
      <el-col :span="4">
        <el-select v-model="buildingSelected"
          clearable
          @clear="changeBuildingSelect"
          @visible-change="clickBuildingSelect"
          placeholder="请选择楼栋">
          <el-option
            v-for="item in buildingList"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-col>
      <el-col :span="4">
        <el-select v-model="cellSelected"
          clearable
          @clear="changeCellSelect"
          @visible-change="clickCellSelect"
          placeholder="请选择单元">
          <el-option
            v-for="item in cellList"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-col>
      <el-col :span="4">
        <el-select v-model="floorSelected"
          clearable
          @clear="changeFloorSelect"
          @visible-change="clickFloorSelect"
          placeholder="请选择楼层">
          <el-option
            v-for="item in floorList"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-col>
      <el-col :span="4">
        <el-select v-model="purposeSelected"
          clearable
          placeholder="请选择用途">
          <el-option
            v-for="item in purposeList"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-col>
      <el-col :span="2">
        <el-button type="primary" class="myBgGreen" @click="fuzzySearch">查 询</el-button>
      </el-col>
    </el-row>
    <el-row class="myMarginTop_10px">
      <el-col :span="24">
        <el-table
          ref="multipleTable"
          v-loading="loading"
          :data="list"
          :border = true
          :stripe = true
          @selection-change="handleSelectionChange">
          <el-table-column
            type="selection"
            width="55">
          </el-table-column>
          <el-table-column
            type="index"
            label="序号"
             :index="indexMethod"
            width="100"
            >
          </el-table-column>
          <el-table-column
            prop="district_name"
            label="所属片区"
            >
          </el-table-column>
          <el-table-column
            prop="area_name"
            label="所属小区"
            >
          </el-table-column>
          <el-table-column
            prop="room_no"
            label="房间编号"
            >
          </el-table-column>
          <el-table-column
            prop="display_room_name"
            label="房间名"
            >
          </el-table-column>
          <el-table-column
            prop="extra_desc"
            label="备注"
            >
          </el-table-column>
          <el-table-column
            label="操作"
            width="100">
            <template scope="scope">
              <el-button @click="getDetail(scope.row.room_id)" class="look" type="text" size="small">查看</el-button> |
              <el-button type="text primary" @click="modify(scope.row.room_id)" class="look" size="small" v-if="isShow(123)">编辑</el-button> 
              <!-- <el-button type="text"  data-toggle="modal" href="#delete" @click="deleteData(scope.row,scope.$index)" class="delete" size="small">删除</el-button> -->
            </template>
          </el-table-column>
        </el-table>
        <div class="block">                    
            <el-pagination
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
              :current-page="page_num"
              :page-sizes="[10, 15]"
              :page-size="page_size"
              layout="total, sizes, prev, pager, next, jumper"
              :total="total">
            </el-pagination>
          </div> 
      </el-col>
    </el-row>

    <!-- 确定删除弹框开始 -->
    <el-dialog title="确定删除?" class="myRoleDialog" :visible.sync="deleteDialog">
      <div>是否要删信息！</div>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" class="myBgGreen" @click="deleteDialog = false">取 消</el-button>
        <el-button type="primary" class="myBgGreen" @click="sureDelete">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 确定删除弹框结束 -->
  </div>
</template>

<style scoped>
  .myContailer{
    background: #fff;
    padding: 10px;
  }
  .myTitleBorderBottom{
    color: #000;
    font-weight: 500;
    font-size: 16px;
    border-bottom: 1px solid #dbdbdb;
  }
  .myBgGreen{
    background: #00c2a9;
    border-color: #00c2a9;
    color: #fff;
    margin-top:2px;
  }
  .myBgGreen:hover{
    background: #33ceba;
  }
  .myTextAlignRight{
    text-align: right;
  }
  .myLineHeight_40px{
    line-height: 40px;
  }
  .myMarginTop_10px{
    margin-top: 10px;
  }

</style>

<script>
  import { getRoomList, purposeList } from '@/api/room';
  import { getFloorList } from '@/api/floor';
  import { getBuildingList } from '@/api/building';
  import { getCellList } from '@/api/cell';
  import { extractAreaIds } from '@/utils/misc';
  import { getRegion } from '@/utils/localstorage';
  export default {
    data(){
      return{
        title:"房间管理",
        list:[],
        total:null, //总条数  
        page_num: null, //当前页码
        page_size: null, //每页显示多少条


        floorList: [],
        floorLoaded: false,
        floorSelected: null,

        cellList: [],
        cellLoaded: false,
        cellSelected: null,

        buildingList: [],
        buildingLoaded: false,
        buildingSelected: null,

        kw:"",
        regionOptions: [],
        regionOptSelected:[],

        purposeSelected: null,
        purposeList: purposeList,

        selectedLd:"-1",
        ldList:[],
        building_id:-1,

        selectedDy:"-1",
        dyList:[],
        cell_id:-1,

        selectedLc:"-1",
        lcList:[],
        floor_id:-1,

        purpose:-1,
        selectedYt:"-1",

        

     
        lookObj:{},
        modifyNum:{},
     
        pageNum: null, //页数
      
        id:null,
        index:null,
        idArr:[],

      
        currentPage:1,  
        pages:1000,
        loading:false,
        multipleSelection: [],
        kw:"",
        deleteDialog:false
    }
    },
    created(){
      document.title=this.title;
      this.regionOptions = getRegion();
      this.regionOptSelected.push(this.regionOptions[0].value,this.regionOptions[0].children[0].value,this.regionOptions[0].children[0].children[0].value);
      
      this.fetchList();
    },
     computed: {
      areaIds() {
        return extractAreaIds(this.regionOptSelected, this.regionOptions);
      }
    },
    methods:{
      fetchList() {
        let vm = this;

        let buildingId = this.buildingSelected;
        let cellId = this.cellSelected;
        let floorId = this.floorSelected;

        if (floorId) {
          buildingId = null;
          cellId = null;
        } else if (cellId && !floorId) {
          buildingId = null;
          floorId = null;
        } else if(buildingId && !cellId && !floorId){
          cellId = null;
          floorId = null;
        }
        const params = {
          building_id: buildingId,
          cell_id: cellId,
          floor_id: floorId,
          kw: this.kw,
          purpose: this.purposeSelected,
          page_size: this.page_size,
          page_num: this.page_num
        };

        vm.loading = true;
        getRoomList(this.areaIds, params)
        .then(response => {
          let res = response.data;
          vm.list = res.rows;
          vm.total = res.total;
          vm.page_num = res.page_num;
          vm.page_size = res.page_size;

          vm.loading = false;
        })
        .catch(error => {
          vm.loading = false;
        });
      },
      fuzzySearch() {
        //this.loading = true;
        this.fetchList();
      },
      handleSizeChange(val) {
        this.page_size = val;
        this.fetchList();
      },
      changeRegionOpt() {
        this.buildingLoaded = false;
        this.buildingList = [];
        this.buildingSelected = null;

        this.changeBuildingSelect();
      },
      handleSizeChange(val) {
        this.page_size = val;
        this.fetchList();
      },
      handleCurrentChange(val) {
        let pageNum=window.localStorage.getItem("pageNum");
        if(pageNum){
          this.page_num = Number(pageNum);
          window.localStorage.removeItem("pageNum");
        }else{
          this.page_num = val;
        }
        this.fetchList();
      },
      add(){
        this.$router.push({
          path:'/fjglComonents',
          query: {type: 'add', pageNum: this.page_num}
        })
      },
      getDetail(id){
        this.$router.push({
          path:'/fjglComonents',
          query: {type: 'info', id: id, pageNum: this.page_num ,areaId:this.areaIds}
        })
      },
      modify(id){
        this.$router.push({
          path:'/fjglComonents',
          query: {type: 'update', id: id, pageNum: this.page_num ,areaId:this.areaIds}
        })
      },
      changeAreaSelect() {
        this.buildingLoaded = false;
        this.buildingSelected = null;
        this.buildingList = [];

        this.changeBuildingSelect();
      },

      clickBuildingSelect(val) {
        if (!val || this.regionOptSelected.length != 3) {
          return;
        }

        let vm = this;
        const params = {
          ril: 'S',
          page_num: 1,
          page_size: 999
        };
        if (!this.buildingLoaded) {
          getBuildingList(this.regionOptSelected[2], params)
          .then(response => {
            vm.buildingList = response.data.rows;
            vm.buildingLoaded = true;
          })
          .catch(error => {

          })
        }
      },
      changeBuildingSelect() {
        this.cellLoaded = false;
        this.cellSelected = null;
        this.cellList = [];

        this.changeCellSelect();
      },
      changeCellSelect() {
        this.floorLoaded = false;
        this.floorSelected = null;
        this.floorList = [];
      },
      changeFloorSelect() {
        this.floorSelected = null;
      },
      clickCellSelect() {
        if (this.buildingSelected == null) {
          return;
        }


        let vm = this;
        const params = {
          ril: 'S',
          building_id: this.buildingSelected,
          page_num: 1,
          page_size: 999
        };
        if (!this.cellLoaded) {
          getCellList(this.areaIds, params)
          .then(response => {
            vm.cellList = response.data.rows;
            vm.cellLoaded = true;
          })
          .catch(error => {

          })
        }
      },
      clickFloorSelect() {
        if (this.cellSelected == null) {
          return;
        }

        let vm = this;
        const params = {
          ril: 'S',
          cell_id: this.cellSelected,
          page_num: 1,
          page_size: 999
        };
        if (!this.floorLoaded) {
          getFloorList(this.areaIds, params)
          .then(response => {
            vm.floorList = response.data.rows;
            vm.floorLoaded = true;
          })
          .catch(error => {

          })
        }
      },
      deleteData:function (datas,index) {
        this.id = datas.room_id;
        this.idArr.push(datas.room_id);
        this.index = index;
        this.lookObj = datas
      },
      
      sureDelete:function(event){
        var pageArr = {
          page_size : this.page_size,
          page_num : this.currentPage,
        }
        if(this.list.length==1){
          var pageArr = {
            page_size : this.page_size,
            page_num : this.currentPage-1
          }
        }

        var arrId = {
          room_ids : this.idArr.join(",")
        }

        optVue.deleteFjData(this,this.index,Tips,pageArr,arrId);
        
      },
    
      allDelete:function(event){
     
        if(this.multipleSelection!=0){
          var pageArr = {
            page_num : this.currentPage-1
          }
          
          var arrId = {
            room_ids : this.idArr.join(",")
          }
          //console.log(JSON.stringify(arrId)) 
          optVue.deleteFjData(this,this.index,Tips,pageArr,arrId);
          
          this.idArr = [];

        }
        else{
          Tips.init({
            type:"fail",
            message:"请选择要删的项",
            class:"on"
          });
        }
      },  
      

      handleSelectionChange(val) {
        this.multipleSelection = val;
        if(val.length!=0){
          for(var i = 0;i<val.length;i++){
            this.idArr.push(val[i].area_id)
          }
        }else{
          this.idArr = [];
        }
      },
      
      indexMethod(index) {
          return (this.page_num-1)*this.page_size + index + 1
      }
    }

  }


</script>



